package com.xiaobao.coalproperty.module.project.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.MainActivity;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.IndexEntity;
import com.xiaobao.coalproperty.module.home.view.PropertyDetailsActivity;
import com.xiaobao.coalproperty.module.project.model.GoodsListEntity;
import com.xiaobao.coalproperty.other.adapter.ProjectAdapter;
import com.xiaobao.coalproperty.other.base.BaseFragment;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

public class ProjectFragment extends BaseFragment<MainActivity> {

    @BindView(R.id.recycler)
    XRecyclerView mXRecyclerView;

    private ProjectAdapter mProjectAdapter;
    private int page = 1;

    @Override
    protected int getContentView() {
        return R.layout.recycler;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mProjectAdapter = new ProjectAdapter(getSuperActivity());
        mProjectAdapter.setOnItemClickListener(mOnItemClickListener);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(getSuperActivity(), LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.setAdapter(mProjectAdapter);
        mXRecyclerView.setLoadingListener(mLoadingListener);

        updateGoodsList();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            page = 1;
            updateGoodsList();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            updateGoodsList();
            mXRecyclerView.loadMoreComplete();
        }
    };

    private ProjectAdapter.onItemClickListener mOnItemClickListener = new ProjectAdapter.onItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getSuperActivity(), PropertyDetailsActivity.class);
            intent.setAction("id");
            intent.putExtra("id", mProjectAdapter.getAdapterData().get(position).getId());
            startActivity(intent);
        }
    };

    private void updateGoodsList() {
        OkHttpUtils.post().url(UrlConstant.URL_GOODS_LIST)
                .params(HttpHelperUtils.getInstance().goodsListParams(page + ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    GoodsListEntity entity = new Gson().fromJson(response, GoodsListEntity.class);
                    if (entity.getCode() == 0) {
                        updateGoodsListSuccess(entity);
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }

    private void updateGoodsListSuccess(GoodsListEntity entity) {
        if (entity.getData().getList().size() > 0) {
            List<IndexEntity.DataBean.GoodsBean> mAdapterData = new ArrayList<>();
            for (int i = 0; i < entity.getData().getList().size(); i++) {
                IndexEntity.DataBean.GoodsBean goodsBean = new IndexEntity.DataBean.GoodsBean();
                goodsBean.setId(entity.getData().getList().get(i).getId());
                goodsBean.setName(entity.getData().getList().get(i).getGoodsname());
                goodsBean.setBuy_num(entity.getData().getList().get(i).getBuy_num());
                goodsBean.setAcreage(entity.getData().getList().get(i).getAcreage());
                goodsBean.setPrice(entity.getData().getList().get(i).getGoodsprice());
                goodsBean.setArea(entity.getData().getList().get(i).getArea());
                goodsBean.setStock(entity.getData().getList().get(i).getStock());
                mAdapterData.add(goodsBean);
            }
            if (page == 1) {
                mProjectAdapter.setAdapterData(mAdapterData);
            } else {
                mProjectAdapter.addAdapterData(mAdapterData);
            }
            page++;
        } else {
            showToast("没有更多了");
        }
    }


}
